<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
   question_group ||= nil
   group = question_group
   if group && hash_get(group, :assessment_question_bank_id)
     question_bank = @banks_hash[hash_get(group, :assessment_question_bank_id)]
   end
  if group
    roles_stuff = %Q|role="region" aria-label="#{t('Question Group')}"|
    question_count = question_bank ? question_bank.assessment_question_count : hash_get(group, :questions, []).count
  end
%>
<div <%= roles_stuff ? roles_stuff : nil %> class="quiz_sortable group_top <%= 'question_bank_top' if question_bank %>" style="<%= "display: none;" unless group %>" id="group_top_<%= hash_get(group, :id, 'template') %>"
  <% if question_bank %>data-bank_question_count="<%= question_count %>"<% end %>
>
  <%= form_for :quiz_group, :url => context_url(@context, :context_quiz_quiz_groups_url, @quiz.id), :html => {:class => "quiz_group_form"} do |f| %>
    <% if can_do(@quiz, @current_user, :update) %>
    <%= render partial: "quizzes/quizzes/move_handle" %>
    <% end %>

  <div style="float: left;">
    <input type="hidden" name="quiz_group[assessment_question_bank_id]" class="bank_id"/>
    <h2 class="group_display name"><%= hash_get(group, :name, t(:default_group_name, "Group")) %></h2>
    <input name="quiz_group[name]" aria-label={{#t}}Group Name{{/t}} type="text" value="Group Name" class="group_edit" value="<%= hash_get(group, :name, t(:default_group_name, "Group")) %>"/>
    <span class="group_display">
      <%= t(:show_questions_and_points, "Pick *%{num_questions}* questions, #%{num_points}# pts per question",
            :num_questions => hash_get(group, :pick_count, 1),
            :num_points => hash_get(group, :question_points, 1),
            :wrapper => {
              '*' => '<span class="pick_count">\1</span>',
              '#' => '<span class="question_points">\1</span>'
            }) %>
    </span>
    <span class="group_edit">
      <%= t(:edit_questions_and_points, "Pick *%{num_questions}* questions, #%{num_points}# pts per question",
            :num_questions => hash_get(group, :pick_count, 1),
            :num_points => hash_get(group, :question_points, 1),
            :wrapper => {
              '*' => "<input name='quiz_group[pick_count]' aria-label='#{h(t('labels.number_of_questions', "Number of Questions"))}' type='text' style='width: 20px;' value='\1'/>",
              '#' => "<input name='quiz_group[question_points]' class='float_value' aria-label='#{h(t('labels.points_per_question', "Points per Question"))}' type='text' style='width: 20px;' value='\1'/>"
            }) %>
    </span>
    <% if !group && @context.grants_right?(@current_user, :read_question_banks) %>
      <div class="group_edit">
        <a href="#" class="icon-search find_bank_link">{{#t}}Link to a Question Bank{{/t}}</a>
      </div>
    <% end %>
  </div>
  <div style="float: right;" class="links">
    <span class="local_group_links">
      <% collapse_title = t('titles.collapse_group', "Collapse Group") %>
      <% expand_title = t('titles.expand_group', "Expand Group") %>
      <% add_question_title = t('titles.add_question_to_group', "Add Question to this Group") %>
      <a href="#" class="collapse_link group_display no-hover" title="<%= collapse_title %>"><i class="icon-arrow-up standalone-icon"></i></a>
      <a href="#" class="expand_link group_display hidden no-hover" title="<%= expand_title %>"><i class="icon-arrow-down standalone-icon"></i></a>
      <a href="#" class="add_question_link group_display no-hover" title="<%= add_question_title %>"><i class="icon-add standalone-icon"></i></a>
    </span>
    <% edit_group_title = t('titles.edit_group_details', "Edit Group Details") %>
    <% delete_group_title = t('titles.delete_group', "Delete Group") %>
    <a href="#" class="edit_group_link group_display no-hover" title="<%= edit_group_title %>"><i class="icon-edit standalone-icon"></i></a>
    <a href="#" class="delete_group_link group_display no-hover" title="<%= delete_group_title %>"><i class="icon-trash standalone-icon"></i></a>
  </div>
  <div class="clear"></div>
  <div class="button-container">
    <button type="button" class="group_edit cancel_button btn btn-small"/>{{#t}}Cancel{{/t}}</button>
    <button type="submit" class="group_edit submit_button btn btn-small btn-primary">{{#t}}Update{{/t}}</button>
  </div>
  <div style="display: none;">
    <a href="<%= context_url(@context, :context_quiz_quiz_group_url, @quiz.id, hash_get(group, :id, "{{ id }}")) %>" class="update_group_url">&nbsp;</a>
    <a href="<%= context_url(@context, :api_v1_context_quiz_group_reorder_url, @quiz.id, hash_get(group, :id, "{{ id }}")) %>" class="reorder_group_questions_url">&nbsp;</a>
  </div>
  <% end %>
</div>
<% if !group || question_bank %>
  <div class="assessment_question_bank" style="<%= hidden unless question_bank %>"><%= before_label(:question_bank, "Questions will be pulled from the bank") %>
    <a href="/<%= question_bank.try(:context_type).try(:underscore).try(:pluralize) || '{{ context_type_string }}' %>/<%= question_bank.try(:context_id) || '{{ context_id }}' %>/question_banks/<%= question_bank.try(:id) || '{{ bank_id }}' %>" class="bank_name bank_name_link" style="font-weight: bold; <%= hidden unless question_bank %>"><%= question_bank.try(:title) %></a>
    <span class="bank_name" style="font-weight: bold; <%= hidden if question_bank %>"><%= question_bank.try(:title) %></span>
  </div>
<% end %>
<div class="insufficient_count_warning" id="insufficient_count_warning_<%= hash_get(group, :id, 'template') %>"  style="<%= hidden %>">
  <i class="icon-warning"></i>
  {{#t}}This question group is set to pick more questions than are available.{{/t}}
</div>
<% hash_get(group, :questions, []).each do |question| %>
  {{> views/display_question", :object => question, :locals => {:in_group => true, :editing => true, :asset_string => "quiz_question_#{question['id']}", :group_id => group["id}}
<% end %>
<div class="group_bottom" style="<%= "display: none;" unless group %>" id="<%= "group_bottom_template" unless group %>">
  &nbsp;
</div>
